package com.twu.biblioteca.view;

import com.twu.biblioteca.controller.UserController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * User: ema93sh
 * Date: 8/17/13
 * Time: 11:55 AM
 */
public class LoginView implements MenuItem {
    private UserController userController;

    public LoginView(UserController userController) {
        this.userController = userController;
    }

    @Override
    public String displayTitle() {
        return "Login/Logout";
    }

    @Override
    public void doWork() throws IOException {

        if (userController.isUserLoggedIn())
        {
           userController.logout();
           System.out.println("Successfully logged out!");
        }
        else {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Username(xxx-xxxx):");
            String username = bufferedReader.readLine();
            System.out.println("Password:");
            String password = bufferedReader.readLine();
            if (userController.login(username, password))
            {
                System.out.println("Successfully Logged In!");
            }
            else {
                System.out.println("Login Failed! Try Again." + username);
            }
        }
    }
}
